package org.openurp.edu.student.info.web.action
import java.beans.Transient
import java.io.File
import java.io.FileOutputStream
import org.beangle.webmvc.api.action.ActionSupport
import org.beangle.data.dao.EntityDao
import org.beangle.data.dao.OqlBuilder
import org.openurp.edu.student.info.model.Student
import org.beangle.webmvc.api.view.View
import org.beangle.webmvc.api.action.ServletSupport
import org.beangle.webmvc.entity.action.RestfulAction
import javax.servlet.http.Part
import org.beangle.webmvc.api.context.Params
import java.io.FileOutputStream
import org.beangle.commons.io.IOs
import org.beangle.commons.lang.Strings
import org.openurp.edu.student.info.model.Attachment
import org.beangle.commons.codec.digest.Digests
import org.openurp.edu.student.info.model.StudentWai
import org.openurp.edu.student.info.service.StudentPortraitService
import org.beangle.webmvc.api.view.Stream
import org.beangle.commons.lang.Numbers
/**
 * @author Administrator
 */
class MyInforAction extends ActionSupport with ServletSupport {
  var studentPortraitService: StudentPortraitService = _
  var entityDao: EntityDao = _

  def index(): String = {
    forward()
  }
  def portrait(): View = {
    val builder = OqlBuilder.from(classOf[Student], "s").where("s.code = :code", request.getSession.getAttribute("code"))
    val users = entityDao.search(builder).head
    println(studentPortraitService.headPortaitBase + users.studentWai.attachment.path)
    println("xx")
    Stream(new File(studentPortraitService.headPortaitBase + users.studentWai.attachment.path))
  }

  def update(): View = {
    val landlord = get("landlord").orNull
    val tel = get("tel").orNull
    val address = get("address").orNull
    val email = get("email").orNull
    val weChat = get("weChat").orNull
    val builder = OqlBuilder.from(classOf[Student], "s").where("s.code = :code", request.getSession.getAttribute("code"))
    val users = entityDao.search(builder).head
    if (Strings.isNotEmpty(tel)) {
      users.tel = tel
    }
    if (Strings.isNotEmpty(weChat)) {
      users.wechat = weChat
    }
    if (Strings.isNotEmpty(landlord)) {
      users.landlord = landlord
    }
    if (Strings.isNotEmpty(email)) {
      users.email = email
    }
    if (Strings.isNotEmpty(address)) {
      users.address = address
    }
    val parts = Params.getAll("attachment").asInstanceOf[List[Part]]
    parts foreach { part =>
      val attachment = new Attachment()
      attachment.fileSize = part.getSize.toInt
      val ext = Strings.substringAfterLast(part.getSubmittedFileName, ".")
      attachment.path = Digests.md5Hex(part.getSubmittedFileName) + (if (Strings.isEmpty(ext)) "" else "." + ext)
      attachment.name = part.getSubmittedFileName
      attachment.updatedAt = new java.util.Date
      IOs.copy(part.getInputStream, new FileOutputStream(studentPortraitService.headPortaitBase + attachment.path))

      if (users.studentWai == null) {
        users.studentWai = new StudentWai
      }
      users.studentWai.attachment = attachment
    }
    if (users.studentWai != null) {
      entityDao.saveOrUpdate(users.studentWai, users)
    } else {
      entityDao.saveOrUpdate(users)
    }
    redirect(to(classOf[HomeAction], "index"), "")
  }

}
